#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 7

void swap(int[], int, int);
void quicksort(int[], int);
void printarray(int[], int);

int main()
{
    int sample_array[] = {7, 5, 4, 6, 3, 8, 2};
    printf("Before sorting: ");
    printarray(sample_array, ARRAY_SIZE);
    quicksort(sample_array, ARRAY_SIZE);
    printf("\nAfter sorting: ");
    printarray(sample_array, ARRAY_SIZE);

    return 0;
}

void quicksort(int v[], int n)
{
    int i, last;
    if (n <= 1)
        return;
    swap(v, 0, rand() % n);
    last = 0;

    for (i = 1; i < n; i++)
        if (v[i] < v[0])
            swap(v, ++last, i);
    swap(v, 0, last);
    quicksort(v, last);
    quicksort(v + last + 1, n - last - 1);
}

void swap(int v[], int i, int j)
{
    int temp;
    temp = v[i];
    v[i] = v[j];
    v[j] = temp;
}

void printarray(int v[], int n)
{
    for (int i = 0; i < n; i++)
        printf("%d ", v[i]);
    printf("\n");
}